package com.yonyou.pmclouds.plan.utils;

import com.yonyou.pmclouds.basecom.util.DateUtils;
import com.yonyou.pmclouds.plan.entity.PlanProgressVO;
import com.yonyou.pmclouds.plan.entity.ProjectScheduleBoardVO;
import com.yonyou.pmclouds.plan.entity.ScheduleItemVO;
import com.yonyou.pmclouds.plan.entity.ScheduleVO;

import java.util.Date;
import java.util.List;

/**
 * @Description:
 * @Author: zhangzhonghao
 * @Date: 2020-03-15
 */
public class ScheduleDateCalculateUtil {

    public static void calculateParentPlanDate(ScheduleItemVO parentItemVO) {
        if (parentItemVO.getChildren() != null && parentItemVO.getChildren().length > 0) {
            Date minStartDate = null;
            Date maxEndDate = null;

            for (ScheduleItemVO childItemVOTemp : parentItemVO.getChildren()) {
                Date planStartDate = childItemVOTemp.getPlanStartDate();
                Date planEndDate = childItemVOTemp.getPlanEndDate();
                if (minStartDate == null || planStartDate.before(minStartDate)) {
                    minStartDate = planStartDate;
                }
                if (maxEndDate == null || planEndDate.after(maxEndDate)) {
                    maxEndDate = planEndDate;
                }
            }

            parentItemVO.setPlanStartDate(minStartDate);
            parentItemVO.setPlanEndDate(maxEndDate);
            parentItemVO.setPlanDays((int) DateUtils.getDaysBetween(minStartDate, maxEndDate));

        }

    }

    public static void calculateParentPlanDate(ScheduleVO scheduleVO) {
        ScheduleItemVO[] itemVOS = scheduleVO.getScheduleItemVOS();
        if(itemVOS == null || itemVOS.length <= 0){
            return;
        }

        Date minStartDate = null;
        Date maxEndDate = null;

        for(ScheduleItemVO itemVO : itemVOS){
            Date planStartDate = itemVO.getPlanStartDate();
            Date planEndDate = itemVO.getPlanEndDate();
            if(minStartDate == null || planStartDate.before(minStartDate)){
                minStartDate = planStartDate;
            }
            if(maxEndDate == null || planEndDate.after(maxEndDate)){
                maxEndDate = planEndDate;
            }
        }

        scheduleVO.setPlanStartDate(minStartDate);
        scheduleVO.setPlanEndDate(maxEndDate);
        scheduleVO.setPlanDays((int) DateUtils.getDaysBetween(minStartDate, maxEndDate));

    }

    public static void calculateParentPlanDate(ProjectScheduleBoardVO scheduleBoardVO) {
        List<ScheduleVO> scheduleVOList = scheduleBoardVO.getScheduleVOList();
        if(scheduleVOList == null || scheduleVOList.size() <= 0){
            return;
        }

        Date minStartDate = null;
        Date maxEndDate = null;

        for(ScheduleVO scheduleVO : scheduleVOList){
            Date planStartDate = scheduleVO.getPlanStartDate();
            Date planEndDate = scheduleVO.getPlanEndDate();
            if(minStartDate == null || planStartDate.before(minStartDate)){
                minStartDate = planStartDate;
            }
            if(maxEndDate == null || planEndDate.after(maxEndDate)){
                maxEndDate = planEndDate;
            }
        }

        scheduleBoardVO.setPlanStartDate(minStartDate);
        scheduleBoardVO.setPlanEndDate(maxEndDate);
        scheduleBoardVO.setPlanDays((int) DateUtils.getDaysBetween(minStartDate, maxEndDate));
    }

    public static void calculateParentDate(ScheduleItemVO itemVO){
        Date minStartDate = null;
        Date maxEndDate = null;

        for(ScheduleItemVO childItemVOTemp : itemVO.getChildren()){
            PlanProgressVO progressVO = childItemVOTemp.getProgressVO();
            if(progressVO == null){
                continue;
            }

            Date startDate = progressVO.getStartDate();
            Date endDate = progressVO.getEndDate();

            if(minStartDate == null || (startDate != null && startDate.before(minStartDate))){
                minStartDate = startDate;
            }
            if(maxEndDate == null || (endDate != null && endDate.after(maxEndDate))){
                maxEndDate = endDate;
            }
        }

        itemVO.getProgressVO().setStartDate(minStartDate);

        // 只有进度为100，才显示实际结束时间
        if(itemVO.getProgressVO() != null && itemVO.getProgressVO().getProgress() == ScheduleItemVO.SCHEDULE_ITEM_COMPLETE){
            itemVO.getProgressVO().setEndDate(maxEndDate);
        }
    }

    public static void calculateParentDate(ScheduleVO scheduleVO){
        Date minStartDate = null;
        Date maxEndDate = null;

        for(ScheduleItemVO childItemVOTemp : scheduleVO.getScheduleItemVOS()){
            Date startDate = childItemVOTemp.getProgressVO().getStartDate();
            Date endDate = childItemVOTemp.getProgressVO().getEndDate();
            if(minStartDate == null || (startDate != null && minStartDate.after(startDate))){
                minStartDate = startDate;
            }
            if(maxEndDate == null || (endDate!= null && maxEndDate.before(endDate))){
                maxEndDate = endDate;
            }
        }

        scheduleVO.setStartDate(minStartDate);

        if(scheduleVO.getProgress() == ScheduleVO.SCHEDULE_ITEM_COMPLETE){
            scheduleVO.setEndDate(maxEndDate);
        }
    }
}
